package cn.zhf.shop.controller;


import cn.zhf.shop.config.GlobalConfig;
import cn.zhf.shop.entity.Financial;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.mapper.FinancialMapper;
import cn.zhf.shop.utils.ChinaPayUtil;
import cn.zhf.shop.utils.DownloadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "交易对账/清算对账模块")
@RequestMapping("/api/financial")
public class FinancialController {

    @Resource
    private GlobalConfig config;
    @Resource
    private FinancialMapper financialMapper;

    @PostMapping("/accountDeal")
    @ApiOperation(value = "交易对账地址Post请求的")
    @ResponseBody
    public void insertAccountDealByPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        request.getMethod();
        String download = request.getParameter("download");
        response.getWriter().write("ChinapayOK");
        String Time = ChinaPayUtil.subDate();
        String filename = Time+"交易对账.zip";
        Financial financial = new Financial();
        financial.setName(Time+"交易对账文件");
        financial.setChinapayDownload(download);
        financial.setDownload(config.hostUrl+"resource/financial/accountDeal/"+filename);
        financial.setUpdateTime(new Date());
        financial.setType(1);
        ChinaPayUtil.Financial(financial,filename,"1",download);
    }

    @GetMapping("/accountDeal")
    @ApiOperation(value = "交易对账地址Get请求的")
    @ResponseBody
    public void insertAccountDealByGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String download = request.getParameter("download");
        response.getWriter().write("ChinapayOK");
        String Time = ChinaPayUtil.subDate();
        String filename = Time+"交易对账.zip";
        Financial financial = new Financial();
        financial.setName(Time+"交易对账文件");
        financial.setChinapayDownload(download);
        financial.setDownload(config.hostUrl+"resource/financial/accountDeal/"+filename);
        financial.setUpdateTime(new Date());
        financial.setType(1);
        ChinaPayUtil.Financial(financial,filename,"1",download);
    }


    @PostMapping("/audit")
    @ApiOperation(value = "清算对账地址Post请求的")
    @ResponseBody
    public void insertAuditByPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String download = request.getParameter("download");
        response.getWriter().write("ChinapayOK");
        String Time = ChinaPayUtil.subDate();
        String filename = Time+"清算对账.zip";
        Financial financial = new Financial();
        financial.setName(Time+"清算对账文件");
        financial.setChinapayDownload(download);
        financial.setDownload(config.hostUrl+"resource/financial/audit/"+filename);
        financial.setUpdateTime(new Date());
        financial.setType(2);
        ChinaPayUtil.Financial(financial,filename,"2",download);

    }

    @GetMapping("/audit")
    @ApiOperation(value = "清算对账地址Get请求的")
    @ResponseBody
    public void insertAuditByGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String download = request.getParameter("download");
        response.getWriter().write("ChinapayOK");
        String Time = ChinaPayUtil.subDate();
        String filename = Time+"清算对账.zip";
        Financial financial = new Financial();
        financial.setName(Time+"清算对账文件");
        financial.setChinapayDownload(download);
        financial.setDownload(config.hostUrl+"resource/financial/audit/"+filename);
        financial.setUpdateTime(new Date());
        financial.setType(2);
        ChinaPayUtil.Financial(financial,filename,"2",download);
    }


    @GetMapping("/selectAll")
    @ApiOperation(value = "查询所有交易")
    public Message selectAll(){
        return Message.success(null).add(financialMapper.selectAll());
    }

    @GetMapping("/selectAllByTime")
    @ApiOperation(value = "根据日期查询交易")
    public Message selectAllByTime(String date){
        return Message.success(null).add(financialMapper.selectAllByUpdateTime(date));
    }


}
